#include "Config.h"
#include <QSlider>
#include <QKeyEvent>
#include "Tank.h"

extern Tank* player[4];

Config::Config(QMediaPlayer* musicPlayer)
{
    box = new QPushButton[2];
    box[0].setParent(this);
    box[1].setParent(this);
    box[0].move(300,500);
    box[0].setText("启用玩家3");
    box[1].move(500,500);
    box[1].setText("启用玩家4");

    for(int i=0;i<2;i++)
    {
        box[i].resize(175,50);
        box[i].setFont(QFont("华文行楷",20,true,false));
    }
    connect(&box[0],&QPushButton::clicked,[](){
       player[2]->isCreated=true;

    });
    connect(&box[1],&QPushButton::clicked,[](){
       player[3]->isCreated=true;

    });
    KeyBtn=new QPushButton [20];
    label = new QLabel[5];
    label[0].setText("玩家1");
    label[1].setText("玩家2");
    label[2].setText("玩家3");
    label[3].setText("玩家4");
    label[4].setText("音量:");
    label[4].setParent(this);
    label[4].setFont(QFont("华文行楷",30,true,false));
    label[4].move(0,45);
    for(int i=0;i<4;i++)
    {
        label[i].setParent(this);
        label[i].setFont(QFont("华文行楷",34,true,false));
        label[i].resize(150,50);
    }
    label[0].move(50,120);
    label[1].move(270,120);
    label[2].move(490,120);
    label[3].move(710,120);
    BtnPressed = new bool [20];
    for(int i=0;i<20;i++)
    {
        BtnPressed[i]=false;
    }
    QPushButton* returnBtn=new QPushButton(this);
    connect(returnBtn,&QPushButton::clicked,this,&QWidget::close);
    returnBtn->move(700,500);
    returnBtn->resize(120,35);
    returnBtn->setText("返回");
    returnBtn->setFont(QFont("华文行楷",20,true,false));
    for(int i=0;i<20;i++)
    {
        KeyBtn[i].setParent(this);
        KeyBtn[i].resize(50,50);
        KeyBtn[i].setCheckable(true);
        KeyBtn[i].setFont(QFont("华文行楷",20,true,false));
    }
    QSlider* slider=new QSlider(this);
    int TempVolume=musicPlayer->volume();
    slider->setValue(TempVolume);
    slider->setMaximum(100);
    slider->setToolTip("拖动滑块改变音量");
    slider->setOrientation(Qt::Horizontal);
    slider->setCursor(Qt::OpenHandCursor);
    slider->resize(400,30);
    slider->move(125,60);
    connect(slider,&QSlider::valueChanged,musicPlayer,&QMediaPlayer::setVolume);

    int line=100;
    KeyBtn[0].setText("上");
    KeyBtn[0].move(90,line+110);
    KeyBtn[1].setText("下");
    KeyBtn[1].move(90,line+180);
    KeyBtn[2].setText("左");
    KeyBtn[2].move(20,line+180);
    KeyBtn[3].setText("右");
    KeyBtn[3].move(160,line+180);
    KeyBtn[4].setText("射击");
    KeyBtn[4].resize(70,50);
    KeyBtn[4].move(80,line+250);

    KeyBtn[5].setText("上");
    KeyBtn[5].move(310,line+110);
    KeyBtn[6].setText("下");
    KeyBtn[6].move(310,line+180);
    KeyBtn[7].setText("左");
    KeyBtn[7].move(240,line+180);
    KeyBtn[8].setText("右");
    KeyBtn[8].move(380,line+180);
    KeyBtn[9].setText("射击");
    KeyBtn[9].resize(70,50);
    KeyBtn[9].move(300,line+250);

    KeyBtn[10].setText("上");
    KeyBtn[10].move(530,line+110);
    KeyBtn[11].setText("下");
    KeyBtn[11].move(530,line+180);
    KeyBtn[12].setText("左");
    KeyBtn[12].move(460,line+180);
    KeyBtn[13].setText("右");
    KeyBtn[13].move(600,line+180);
    KeyBtn[14].setText("射击");
    KeyBtn[14].resize(70,50);
    KeyBtn[14].move(520,line+250);

    KeyBtn[15].setText("上");
    KeyBtn[15].move(750,line+110);
    KeyBtn[16].setText("下");
    KeyBtn[16].move(750,line+180);
    KeyBtn[17].setText("左");
    KeyBtn[17].move(680,line+180);
    KeyBtn[18].setText("右");
    KeyBtn[18].move(820,line+180);
    KeyBtn[19].setText("射击");
    KeyBtn[19].resize(70,50);
    KeyBtn[19].move(740,line+250);
    for(int i=0;i<20;i++)
    {
        connect(&KeyBtn[i],&QPushButton::clicked,[this,i](){
            this->BtnPressed[i]=true;
        });
    }
}

void Config::keyPressEvent(QKeyEvent *event)
{
    for(int i=0;i<20;i++)
    {
        if(this->BtnPressed[i] == true)
        {

            if(i % 5 == 0)
            {
                player[i/5]->my_up=event->key();
           }
            if(i%5 == 1)
            {
                player[i/5]->my_down=event->key();
            }
            if(i%5 == 2)
            {
                player[i/5]->my_left=event->key();
            }
            if(i%5 == 3)
            {
                player[0]->my_right=event->key();
            }
            if(i%5 == 4)
            {
                player[i/5]->my_shoot=event->key();
            }
            BtnPressed[i]=false;

        }
    }
}

Config::~Config()
{
    delete []BtnPressed;
}
